package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.BizEnums;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.FpShopInformationDTO;
import com.financing.wap.dto.FpShopProjectInformationDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpShopInformationService;
import com.financing.wap.service.IFpShopProjectInformationService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpShopInformationController")
@RequestMapping("/api/fpShopInformation")
public class FpShopInformationController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IFpShopInformationService fpShopInformationService;
	@Autowired
	private IFpShopProjectInformationService fpShopProjectInformationService;
	@RequestMapping("/listFpShopInformation")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopInformationDTO fpShopInformation,Pager pager){
		ResultMap result = new ResultMap();

		try{
			logger.info("query fpShopInformation list begin .......");

			//			fpShopInformation.setBegin(pager.getStartIndex());
			//			fpShopInformation.setPageSize(pager.getPageSize());

			int count = fpShopInformationService.queryListCount(fpShopInformation);

			pager.setTotalCount(count);

			List<FpShopInformationDTO> fpShopInformationList = null;

			if(count > 0){
				fpShopInformationList = fpShopInformationService.queryList(fpShopInformation);
			}else{
				fpShopInformationList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopInformationList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query fpShopInformation list end .......");
		}catch(Exception e){
			log.error("query fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/addFpShopInformation")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("add fpShopInformation  begin .......");

			DTOdateSetUtil.setCreateInfo(fpShopInformation);
			DTOdateSetUtil.setUpdateInfo(fpShopInformation);

			int id = fpShopInformationService.insert(fpShopInformation);
			
			result.setAttribute("id", id);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("add fpShopInformation list end .......");
		}catch(Exception e){
			log.error("add fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}


	@RequestMapping("/detailFpShopInformation")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("query detail fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			FpShopInformationDTO dto = fpShopInformationService.loadById(id);

			result.setAttribute("dto",dto);
			
			UserDTO user = getSession(true).getUser();
			if(user!=null){
				FpShopProjectInformationDTO spi = new FpShopProjectInformationDTO();
				spi.setULoginName(user.getLoginName());
				spi.setInformationId(id);
				
				List<FpShopProjectInformationDTO> spis = fpShopProjectInformationService.queryList(spi);
				if(spis.size()>0){
					result.setAttribute("pro", spis.get(0));
				}
			}
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query detail fpShopInformation  end .......");
		}catch(Exception e){
			log.error("query detail fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/deleteFpShopInformation")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("delete fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			fpShopInformationService.delete(id);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("delete fpShopInformation  end .......");
		}catch(Exception e){
			log.error("delete fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/updateFpShopInformation")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("update fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(fpShopInformation);

			fpShopInformationService.update(fpShopInformation);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("update fpShopInformation list end .......");
		}catch(Exception e){
			log.error("update fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/updateForceFpShopInformation")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("updateForce fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(fpShopInformation);

			fpShopInformationService.updateForce(fpShopInformation);


			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("updateForce fpShopInformation list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/queryInformation")
	@ResponseBody
	public ResultMap queryInformation(HttpServletRequest request, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("query fpShopInformation list begin .......");


			String type = fpShopInformation.getType();
			fpShopInformation.setUpStatus(BizEnums.UpOrDownEnums.ZIXUN_UP);
			
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//			String curdate = sdf.format(new Date());
//			Date date =  sdf.parse(curdate);
			fpShopInformation.setReleaseTime(new Date());
			if(StringUtils.equals(type, BizEnums.InformationType.INFORMATION_IMMEDIATE)) {
				//查询即时快讯
				fpShopInformation.setType(BizEnums.InformationType.INFORMATION_IMMEDIATE);
				List<FpShopInformationDTO> immediateList = fpShopInformationService.queryCurdayInformations(fpShopInformation);

				result.setAttribute("immediateList", immediateList);
			} else {
				//查询即时快讯
				fpShopInformation.setType(BizEnums.InformationType.INFORMATION_IMMEDIATE);
				List<FpShopInformationDTO> immediateList = fpShopInformationService.queryCurdayInformations(fpShopInformation);

				//资讯简报
				fpShopInformation.setType(BizEnums.InformationType.INFORMATION_BIREF);
				List<FpShopInformationDTO> birefList = fpShopInformationService.queryCurdayInformations(fpShopInformation);

				//热点资讯
				fpShopInformation.setType(BizEnums.InformationType.INFORMATION_HOT);
				List<FpShopInformationDTO> hotList = fpShopInformationService.queryCurdayInformations(fpShopInformation);

				//早鸟理财师驿站
				fpShopInformation.setType(BizEnums.InformationType.INFORMATION_MORING);
				List<FpShopInformationDTO> moringList = fpShopInformationService.queryCurdayInformations(fpShopInformation);
				
				
				//查询首页产品关联
				UserDTO user = SessionUtil.getSession().getUser();
				if(user!=null){
					FpShopProjectInformationDTO fp = new FpShopProjectInformationDTO();
					fp.setULoginName(user.getLoginName());
					fp.setInformationId(0);
					List<FpShopProjectInformationDTO> ffs= fpShopProjectInformationService.queryList(fp);
					
					
					result.setAttribute("pro", ffs);
				}
				
				result.setAttribute("immediateList", immediateList);
				result.setAttribute("birefList", birefList);
				result.setAttribute("hotList", hotList);
				result.setAttribute("moringList", moringList);
			}
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query fpShopInformation list end .......");
		}catch(Exception e){
			log.error("query fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	/**
	 * 浏览量统计
	 * 
	 * @param request
	 * @param fpShopInformation
	 * @return
	 */
	@RequestMapping("/countInformation")
	@ResponseBody
	public ResultMap countInformation(HttpServletRequest request, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			if(fpShopInformation.getId()==null) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopInformationDTO fsi = fpShopInformationService.loadById(fpShopInformation.getId());
			if(fsi.getBrowseNum() == null) {
				fsi.setBrowseNum(1);
			} else {
				fsi.setBrowseNum(fsi.getBrowseNum()+1);
			}
			fpShopInformationService.update(fsi);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
		}catch(Exception e){
			log.error("query fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
	
		return result;
	}
}
